package chances.wechat.api;

public class TokenRefresh /*implements Runnable */{
	
//	private TokenSet tokenSet;
	
//	private static final String FIX = "|";
	
	//private static final Logger wxgw = LoggerFactory.getLogger("wxgwLogger");
	
//	public TokenRefresh(TokenSet tokenSet) {
//		this.tokenSet = tokenSet;
//	}
//
//	public void run() {
//		getToken();
//		getJSToken();
//	}
//	
//	public void getToken() {
//		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + 
//				tokenSet.getAppId() + "&secret=" + tokenSet.getSecret();
//		String resultJson = HttpUtils.get(url, null);
//		Map<String, Object> response = JSONUtils.toMap(resultJson);
//		String accessToken = (String) response.get("access_token");
//		if (null == accessToken) {
//			throw new WechatException("微信公众号token获取出错，错误信息:" + response.get("errcode") +
//					"," + response.get("errmsg"));
//		}
//		logToken(tokenSet.getAppId(), accessToken, resultJson);
//		tokenSet.setAccessToken(accessToken);
//		Integer time = (Integer) response.get("expires_in");
//		tokenSet.setAccessTokenStartTime(time);
//		tokenSet.setUpdateTime(new Date());
//	}
//	
//	public void getJSToken() {
//		String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + 
//				tokenSet.getAccessToken() + "&type=jsapi";
//		String resultJson = HttpUtils.get(url, null);
//		Map<String, Object> response = JSONUtils.toMap(resultJson);
//		String ticket = (String) response.get("ticket");
//		if (StringUtils.isBlank(ticket)) {
//			throw new WechatException(
//					"微信公众号jsToken获取出错，错误信息:" + response.get("errcode") +
//					"," + response.get("errmsg"));
//		}
//		logJsapiTicket(tokenSet.getAccessToken(), ticket, resultJson);
//		tokenSet.setJsApiTicket(ticket);
//		Integer time = (Integer) response.get("expires_in");
//		tokenSet.setJsTokenStartTime(time);
//		tokenSet.setUpdateTime(new Date());
//	}
//	
//	public void logToken(String appId, String accessToken, String resultJson) {
//		StringBuffer sb = new StringBuffer();
//		sb.append(appId).append(FIX)
//		  .append(accessToken).append(FIX)
//		  .append(resultJson);
//		wxgw.info(sb.toString());
//	}
//	
//	public void logJsapiTicket(String accessToken, String jsApiTicket, String resultJson) {
//		StringBuffer sb = new StringBuffer();
//		sb.append(tokenSet.getAppId()).append(FIX)
//		  .append(accessToken).append(FIX)
//		  .append(jsApiTicket).append(FIX)
//		  .append(resultJson);
//		wxgw.info(sb.toString());
//	}
//
//	public TokenSet getTokenSet() {
//		return tokenSet;
//	}
//
//	public void setTokenSet(TokenSet tokenSet) {
//		this.tokenSet = tokenSet;
//	}
}